/**
 * 
 */
package org.xlab.baseframework.listener;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.xlab.baseframework.common.entity.AbstractSystemBean;
import org.xlab.baseframework.exception.RootUserNotFoundException;
import org.xlab.baseframework.exception.SystemParameterDuplicateException;
import org.xlab.baseframework.exception.SystemParameterNotFoundException;
import org.xlab.baseframework.exception.SystemRuntimeException;
import org.xlab.baseframework.exception.ValidateFailedException;
import org.xlab.baseframework.service.api.ISystemParameterService;
import org.xlab.baseframework.service.spi.ISystemInitializationListener;
import org.xlab.baseframework.system.PlatformDeclare;

/**
 * @author makubex
 * 
 */
@Component
public class SystemParameterGenerater extends AbstractSystemBean implements
		ISystemInitializationListener {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 3873905046363185633L;
	private static final Logger log = Logger
			.getLogger(SystemParameterGenerater.class);
	@Autowired(required = true)
	@Qualifier("bpf_systemParameterService")
	private ISystemParameterService systemParameterService;

	/**
	 * @see org.xlab.baseframework.service.spi.ISystemInitializationListener#getSequence()
	 */
	public int getSequence() {
		return PlatformDeclare.SEQUENCE_OF_SYSTEM_INITIALIZATION;
	}

	/**
	 * @return the systemParameterService
	 */
	public ISystemParameterService getSystemParameterService() {
		return systemParameterService;
	}

	/**
	 * @see org.xlab.baseframework.service.spi.ISystemInitializationListener#onSystemStarted()
	 */
	public void onSystemStarted() throws SystemRuntimeException {
		log.debug("Listener [" + this.getClass().getName()
				+ "] is running......");
		try {
			systemParameterService.findByCode(
					PlatformDeclare.PARAM_WEBSERVICE_TIMEOUT_CODE, false);
			log.debug("Parameter ["
					+ PlatformDeclare.PARAM_WEBSERVICE_TIMEOUT_CODE
					+ "] had been created already!");
		} catch (SystemParameterNotFoundException e) {
			try {
				log.debug("Now creating system parameter ["
						+ PlatformDeclare.PARAM_WEBSERVICE_TIMEOUT_CODE + "]");
				systemParameterService.createAdvancedParameter(
						PlatformDeclare.PARAM_WEBSERVICE_TIMEOUT_CODE,
						PlatformDeclare.PARAM_WEBSERVICE_TIMEOUT_DEFAULT,
						PlatformDeclare.PARAM_WEBSERVICE_TIMEOUT_DESC, null,
						super.userService.getSystemRootUser());
			} catch (SystemParameterDuplicateException e1) {
				e1.printStackTrace();
			} catch (RootUserNotFoundException e1) {
				e1.printStackTrace();
			} catch (ValidateFailedException e1) {
				e1.printStackTrace();
			}
		}
		try {
			systemParameterService.findByCode(
					PlatformDeclare.PARAM_WEBSERVICE_SINGLESESSION_CODE, false);
			log.debug("Parameter ["
					+ PlatformDeclare.PARAM_WEBSERVICE_SINGLESESSION_CODE
					+ "] had been created already!");
		} catch (SystemParameterNotFoundException e) {
			try {
				log.debug("Now creating system parameter ["
						+ PlatformDeclare.PARAM_WEBSERVICE_SINGLESESSION_CODE
						+ "]");
				systemParameterService.createAdvancedParameter(
						PlatformDeclare.PARAM_WEBSERVICE_SINGLESESSION_CODE,
						PlatformDeclare.PARAM_WEBSERVICE_SINGLESESSION_DEFAULT,
						PlatformDeclare.PARAM_WEBSERVICE_SINGLESESSION_DESC,
						null, super.userService.getSystemRootUser());
			} catch (SystemParameterDuplicateException e1) {
				e1.printStackTrace();
			} catch (RootUserNotFoundException e1) {
				e1.printStackTrace();
			} catch (ValidateFailedException e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * @param systemParameterService
	 *            the systemParameterService to set
	 */
	public void setSystemParameterService(
			ISystemParameterService systemParameterService) {
		this.systemParameterService = systemParameterService;
	}

}
